//    Copyright (C) Kherty.  All rights reserved.
using System;
using OpenLS.Core.Serialization;

namespace OpenLS.Spreadsheet
{
    ///<summary>
    /// Conditional formatting rule applied when a cell contains a given text.
    ///</summary>
    public class ContainsTextFormattingRule : StandardConditionalFormattingRule
    {
        internal override ConditionalFormattingType Type
        {
            get { return ConditionalFormattingType.ContainsText; }
        }

        internal override void WriteInner2(WriteContext context)
        {
            context.SetEnum("operator", ConditionalFormattingOperator.ContainsText);
          //\\  ConditionalFormatting conditionalFormatting = context.GetData<ConditionalFormatting>();
          //\\  if (conditionalFormatting != null && conditionalFormatting.Range != null)
            //\\    Formula1 = "=NOT(ISERROR(SEARCH(\"" + Text + "\","+conditionalFormatting.Range.TopLeftCell.Label+")))";
        }
        internal override void PrepareBeforeWrite(ConditionalFormatting conditionalFormatting, ConditionalFormattingRule.FormatType formatType)
        {
            Formula1 = "=NOT(ISERROR(SEARCH(\"" + this.EscapedText+ "\"," + conditionalFormatting.Range.TopLeftCell.Label + ")))";
        }

        internal override bool Apply(Cell cell)
        {
            bool isOk = false;
            var v = cell.Value as string;
            if (v != null)
                isOk = v.Contains(Text);
            return ApplyStyle(cell, isOk);
        }
    }
}
